package cn.community.system.taskWork;

import cn.community.system.domain.Activity;
import cn.community.system.domain.ActivityRecord;
import cn.community.system.domain.Association;
import cn.community.system.domain.Member;
import cn.community.system.service.IActivityRecordService;
import cn.community.system.service.IActivityService;
import cn.community.system.service.IAssociationService;
import cn.community.system.service.IMemberService;
import cn.community.system.util.Const;
import cn.community.system.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Component
public class FlightTrainTask {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityRecordService activityRecordService;

    @Autowired
    private IAssociationService associationService;

    @Autowired
    private IMemberService memberService;

      /*
    如：“0/5 * * * * ?”

　　CronTrigger配置完整格式为： [秒] [分] [小时] [日] [月] [周] [年]

        序号	    说明	    是否必填	    允许填写的值	    允许的通配符
        1	    秒	    是	        0-59	            , - * /
        2	    分	    是	        0-59	            , - * /
        3	    小时	    是	        0-23	            , - * /
        4	    日	    是	        1-31	            , - * ? / L W
        5	    月	    是	        1-12或JAN-DEC	    , - * /
        6	    周	    是	        1-7或SUN-SAT	        , - * ? / L W
        7	    年	    否	        empty 或1970-2099	, - * /
     */

    /**
     *  判断活动是否已经结束
     *  1、活动结束，统计每年的活动总个数和总天数
     *  2、活动结束，将成员获得的活动时长和次数增加
     */
    // @Scheduled(cron = "0/5 * * * * ? ") // 间隔5秒执行
    // @Scheduled(cron = "0 0/2 * * * ? ") // 间隔2分钟执行
    @Scheduled(cron = "0 0/30 0 * * ? ") //每30分钟执行一次
    public void isEnd() {
        // 1、活动结束，统计社团每年的活动总个数和总天数，以及学生获得的活动时长和次数增加
        // A、找出从现在开始的前20分钟内结束的活动
        Date date = new Date();
        date.setMinutes(date.getMinutes() - 20);
        List<Activity> activitys = activityService.getByTime(date,new Date());

        for (Activity activity : activitys) {
            Double allTime = activity.getAllTime();
            Long sponsorId = activity.getSponsorId();
            // B、获取活动的时长，设置社团每年的活动总个数和总天数
            Association association = associationService.get(sponsorId);
            System.out.println("之前的社团每年的活动总个数:"+association.getActivityNumber()+"，总天数："+association.getDay());
            association.setActivityNumber(association.getActivityNumber()+1);
            association.setDay(association.getDay() + allTime/24);
            System.out.println("社团每年的活动总个数:"+association.getActivityNumber()+"，总天数："+association.getDay());
            // 判断今天是否为今年的最后一天
            if(this.isLastDay(new Date())){
                association.setActivityNumber(0);
                association.setDay(0.0);
            }
            associationService.updateActivityNumberAndDay(association);

            // C、获取报名该活动的学生，设置学生的活动时长和次数
            List<ActivityRecord> activityRecord = activityRecordService.getByApplySuccess(activity.getId());
            for (ActivityRecord record : activityRecord) {
                Long applicantId = record.getApplicantId();
                Member member = memberService.get(applicantId);
                System.out.println("学号："+member.getNumber()+"，"+member.getUsername()+"的活动总个数:"+member.getActivities()+"，总时长："+member.getActivityTime());
                member.setActivities(member.getActivities() + 1);
                member.setActivityTime(member.getActivityTime() + allTime);
                memberService.updateActivitiesAndTime(member);
                System.out.println("学号："+member.getNumber()+"，"+member.getUsername()+"的活动总个数:"+member.getActivities()+"，总时长："+member.getActivityTime());
            }
        }
    }

    private boolean isLastDay(Date date){
        Date lastDay = new Date();
        lastDay.setMonth(12);
        lastDay.setDate(31);
        lastDay.setHours(23);
        lastDay.setMinutes(59);
        lastDay.setSeconds(59);
        return date.equals(lastDay);
    }
}

